<?php

namespace IMooc;

use App\Model\MUser;

/**
 * 迭代器模式：用户
 */
class AllUser implements \Iterator
{
    protected mixed $ids;
    protected array $data = [];
    protected int $index;

    function __construct()
    {
        $db = Factory::getDatabase();
        $result = $db->query("select id from user");
        $this->ids = $result->fetch_all(MYSQLI_ASSOC);
    }

    function current(): MUser
    {
        $id = $this->ids[$this->index]['id'];
        return Factory::getUser($id);
    }

    function next(): void
    {
        $this->index++;
    }

    function valid(): bool
    {
        return $this->index < count($this->ids);
    }

    function rewind(): void
    {
        $this->index = 0;
    }

    function key(): int
    {
        return $this->index;
    }
}